package cn.zcj.studytest.controller;

import cn.zcj.studytest.ThrowData.Result;
import cn.zcj.studytest.domain.SignUp;
import cn.zcj.studytest.service.SignUpService;
import cn.zcj.studytest.service.TokenService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/admin")
@Slf4j
public class SignUpController {
    @Resource
    private SignUpService signUpService;
    @Resource
    private TokenService tokenService;

    // 注册
    @PostMapping("/signup")
    public Result register(@RequestBody SignUp signUp) {
        return signUpService.sinUpInsert(signUp);
    }

    // 登录
    @PostMapping("/login")
    public Result<Long> signIn(@RequestBody SignUp signUp, HttpServletResponse response) {
        Result<Long> result = new Result<>();
        Result<Long> login = signUpService.login(signUp);
        String token = tokenService.getToken(login.getData());
        Cookie cookie = new Cookie("Token", token);
        cookie.setPath("/");
        response.addCookie(cookie);
        return login;
    }

    // 获取用户信息
    @PostMapping("/getUserInfo")
    public Result getUserInfo(@RequestBody SignUp signUp) {
        return signUpService.getUserInfo(signUp);
    }

    // 充值
    @PostMapping("/recharge")
    public Result recharge(@RequestBody SignUp signUp) {
        return signUpService.recharge(signUp);
    }
}
